﻿using NewLife.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfBill.Common;
using WpfBill.Data;

namespace WpfBill.Controls
{
    /// <summary>
    /// UCEnterprise.xaml 的交互逻辑
    /// </summary>
    public partial class UCEnterprise : UserControl
    {

        public UCEnterprise()
        {
            InitializeComponent();
            Init();
        }
        private void Init()
        {
            ucPage.PageChanged += UcPage_PageChanged;
            ucPage.PageSizeChanged += UcPage_PageSizeChanged;
            QueryData(0);

        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rp"></param>
        private void QueryData(int page)
        {
            try
            {
                int rp = ucPage.PageSize;
                long count = Enterprise.FindCount();
                ucPage.Count = (int)count;
                PageParameter pageParam = new PageParameter() { PageIndex = page, PageSize = rp };

                XCode.Expression where = new XCode.Expression();
                string name = txtName.Text.Trim();
                string person = txtManager.Text.Trim();
                if (!string.IsNullOrEmpty(name))
                    where &= Enterprise._.Name.Contains(name);
                if (!string.IsNullOrEmpty(person))
                    where &= Enterprise._.Person.Contains(person);
                var list = Enterprise.FindAll(where, pageParam);
                this.dgData.ItemsSource = list;
            }
            catch (Exception ex)
            {

            }
        }

        private void TxtName_TextChanged(object sender, TextChangedEventArgs e)
        {
            QueryData(0);
        }

        private void TxtManager_TextChanged(object sender, TextChangedEventArgs e)
        {
            QueryData(0);
        }

        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            GUI.WinEnterprise win = new GUI.WinEnterprise();
            win.ShowDialog();
        }

        private void UcPage_PageChanged(object sender, int page, int rp)
        {
            QueryData(page);
        }

        private void UcPage_PageSizeChanged(object sender, int page, int rp)
        {
            QueryData(page);
        }


        private void dgBtnDel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var proInfo = dgData.SelectedItem as Enterprise;
                if (proInfo != null)
                    proInfo.Delete();
                QueryData(ucPage.Page);
            }
            catch (Exception ex)
            {

            }
        }

        private void dgBtnProjectView_Click(object sender, RoutedEventArgs e)
        {
            Enterprise enterprise = dgData.SelectedItem as Enterprise;
            AppHelper.Main.ShowProject(enterprise);
        }

        private void dgBtnView_Click(object sender, RoutedEventArgs e)
        {

        }

        private void DgData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            try
            {
                var proInfo = e.Row.Item as Enterprise;
                int index = e.Column.DisplayIndex;
                string header = ((System.Windows.Data.Binding)((System.Windows.Controls.DataGridBoundColumn)e.Column).Binding).Path.Path;// e.Column.Header.ToString();
                string value = (e.Column.GetCellContent(e.Row) as TextBox).Text;
                if (proInfo[header].ToString() != value)
                {
                    proInfo[header] = value;
                }
                proInfo.Save();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
        }
    }
}
